package dao;

import beans.Account;
import helper.ReadConfig;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class Student {

    public List<String> listStudentByClass(String class_id) {
        try {
            List<String> listAcc = new ArrayList<String>();
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "select * from tbl_ClassStudent where Class_Id = " + Integer.parseInt(class_id);
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sSQL);
            while (rs.next()) {
                listAcc.add(rs.getString("Acc_Id"));
            }
            rs.close();
            stm.close();
            return listAcc;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean deleteStudent(String acc_id, String class_id) {
        try {
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL1 = "DELETE FROM tbl_Assignment WHERE Acc_Id = " + Integer.parseInt(acc_id)
                    + " and Class_Id = " + Integer.parseInt(class_id);
            String sSQL = "DELETE FROM tbl_ClassStudent WHERE Acc_Id = " + Integer.parseInt(acc_id)
                    + " and Class_Id = " + Integer.parseInt(class_id);
            Statement stm = con.createStatement();
            Statement stm1 = con.createStatement();
            int rs1 = stm1.executeUpdate(sSQL1);
            int rs = stm.executeUpdate(sSQL);
            if (rs == 1 && rs1 != 0) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public List<String> listClassByStudent(String acc_id) {
        try {
            List<String> listAcc = new ArrayList<String>();
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "select * from tbl_ClassStudent where Acc_Id = " + Integer.parseInt(acc_id);
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery(sSQL);
            while (rs.next()) {
                listAcc.add(rs.getString("Class_Id"));
            }
            rs.close();
            stm.close();
            return listAcc;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean addStudentinClass(String acc_id, String class_id) {
        try {
            ReadConfig readConf = new ReadConfig();
            Connection con = readConf.getDBConnection();
            String sSQL = "insert into tbl_ClassStudent "
                    + "values(" + Integer.parseInt(acc_id) + "," + Integer.parseInt(class_id) + ")";
            Statement stm = con.createStatement();
            int rs = stm.executeUpdate(sSQL);
            if (rs == 1) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
